module ApplicationHelper
  def author_name(data)
    data.author.nil? ? 'System' : data.author.name_display
  end
  def user_name(user)
    user.nil? ? 'System' : user.name_display
  end
  def time_text(time)
    time.nil? ? '' : time.localtime.to_s(:db)
  end
  def date_text(time)
    time.nil? ? '' : time.localtime.strftime('%F')
  end

  def js_tag(text, action)
    #js_text = action.to_a.map { |a|
    #  create_javascript(a[0].to_s.strip, a[1])
    #}.join(';')
    raw "<a href=\"#\" onclick=\"#{create_js(action)}\">#{text}</a>"
  end

  def js_tag_option(text, action, exception)
    if exception
      text
    else
      js_tag(text, action)
    end
  end

  def js_script(action)
    raw "<script type=\"text/javascript\">#{create_js_true(action)}</script>"
  end

  def create_js_true(action)
    action.to_a.map { |a|
      create_javascript(a[0].to_s.strip, a[1])
    }.join(';')
  end

  def create_js(action)
    create_js_true(action) + ";return false"
  end

  def hide_style
    "display:none"
  end

  def set_alias(name, name_alias)
    raw "<script type=\"text/javascript\">
        aliasTable['#{name_alias}'] = '#{name}'</script>"
  end

  def div_tag(name_profix, name_obj)
    raw "<div id=\"#{name_profix}#{name_obj.id}\">"
  end
  def div_tag_hidden(name_profix, name_obj)
    raw "<div style=\"display:none\" id=\"#{name_profix}#{name_obj.id}\">"
  end

  def really_submit_tag
    hidden_field_tag :really_submit, ''
  end
private
  def create_javascript(type, aim)
    if type == 'show'
      "cjs_show('#{aim}')"
    elsif type == 'hide'
      "cjs_hide('#{aim}')"
    elsif type == 'switch'
      "cjs_switch('#{aim}')"
    else
      "cjs_#{type}('#{aim}')"
    end
  end
end
